/**
 * 
 */
package org.sevaapp.security.domain;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Transient;

import org.sevaapp.dao.DaoException;
import org.sevaapp.domain.BaseDomainObject;
import org.sevaapp.security.dao.AuthorityDao;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Srini
 *
 */
@MappedSuperclass
public abstract class BaseAuthority extends BaseDomainObject {
	
	@Id
    @Column(name="username")
	private String loginId;
	
    @Column(name="authority")
	private String role;
    
    @ManyToOne
    @JoinColumn(name="parent", referencedColumnName="username")
    private User parent;

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	@Autowired
	@Transient
	private AuthorityDao dao;

	// CRUD Methods
	public void add() {
		try {
			dao.add((Authority) this);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void add(Authority inputData) {
		try {
			dao.add((Authority) inputData);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void delete(String id) {
		try {
			dao.delete(id);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void update(Authority inputData) {
		try {
			dao.update(inputData);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public Authority load(String id) {
		Authority newDomainObj = null;
		try {
			newDomainObj = dao.load(id);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
		return newDomainObj;
	}
}
